package com.brillilab.service.core.experiments.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.experiments.PlateGridTimerMapper;
import com.brillilab.dao.mapper.experiments.PlateMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.experiments.Plate;
import com.brillilab.domain.po.experiments.PlateGridTimer;
import com.brillilab.domain.vo.experiments.PlateTimerVo;
import com.brillilab.service.core.experiments.IPlateGridTimerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class PlateGridTimerService implements IPlateGridTimerService {

    @Resource
    private PlateGridTimerMapper plateGridTimerMapper;
    @Resource
    private PlateMapper plateMapper;

    @Override
    public boolean insert(PlateGridTimer gridTimer) {
        return plateGridTimerMapper.insert(gridTimer) > 0;
    }

    @Override
    public int selectCountOnePlate(Long plateId) {
        return plateGridTimerMapper.selectCount(
                new LambdaQueryWrapper<PlateGridTimer>()
                        .eq(PlateGridTimer::getPlateId,plateId));
    }

    @Override
    public PlateGridTimer selectOne(Long plateId,Long gridId,Long timerId) {
        return plateGridTimerMapper.selectOne(
                new LambdaQueryWrapper<PlateGridTimer>()
                        .eq(PlateGridTimer::getPlateId,plateId)
                        .eq(PlateGridTimer::getPlateGridId,gridId)
                        .eq(PlateGridTimer::getStepTimerId,timerId));
    }

    @Override
    public List<PlateGridTimer> selectList(Long plateId) {
        return plateGridTimerMapper.selectList(new LambdaQueryWrapper<PlateGridTimer>().eq(PlateGridTimer::getPlateId,plateId));
    }

    @Override
    public List<PlateTimerVo> selectVoList(Long plateId,Long gridId) {
        return plateGridTimerMapper.selectVoList(plateId,gridId);
    }

    @Override
    public boolean deleteByStepTimerId(Long timerId){
        LambdaQueryWrapper<PlateGridTimer> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PlateGridTimer::getStepTimerId,timerId);
        return plateGridTimerMapper.delete(wrapper) > 0;
    }

    @Override
    public boolean deleteByPlateId(Long plateId) {
        return plateGridTimerMapper.delete(
                new LambdaQueryWrapper<PlateGridTimer>()
                        .eq(PlateGridTimer::getPlateId,plateId)) > 0;
    }

    @Override
    public List<PlateGridTimer> selectList(Long stepTimerId,List<Long> gridIds) {
        return plateGridTimerMapper.selectList(
                new LambdaQueryWrapper<PlateGridTimer>()
                        .eq(PlateGridTimer::getStepTimerId,stepTimerId)
                        .in(PlateGridTimer::getPlateGridId,gridIds));
    }

    @Override
    public int selectCountOneGrid(Long gridId) {
        return plateGridTimerMapper.selectCount(
                new LambdaQueryWrapper<PlateGridTimer>()
                        .eq(PlateGridTimer::getPlateGridId,gridId));
    }

    @Override
    public boolean deleteById(Long id) {
        return plateGridTimerMapper.deleteById(id) > 0;
    }

    @Override
    public List<PlateGridTimer> selectList(List<Long> gridIds) {
        if(gridIds.size()>0){
            return plateGridTimerMapper.selectList(
                    new LambdaQueryWrapper<PlateGridTimer>()
                            .in(PlateGridTimer::getPlateGridId,gridIds));
        }
        return new ArrayList<>();
    }

    @Override
    public int insertBatch(List<PlateGridTimer> timers) {
        return plateGridTimerMapper.insertBatch(timers);
    }

    @Override
    public int deleteInGridIds(Long stepTimerId,List<Long> gridIds) {
        if(gridIds.size()>0){
            return plateGridTimerMapper.delete(
                    new LambdaQueryWrapper<PlateGridTimer>()
                            .eq(PlateGridTimer::getStepTimerId,stepTimerId)
                            .in(PlateGridTimer::getPlateGridId,gridIds));
        }
        return 0;
    }

    @Override
    public int deleteInPlate(Long stepTimerId,Long plateId) {
        return plateGridTimerMapper.delete(
                new LambdaQueryWrapper<PlateGridTimer>()
                        .eq(PlateGridTimer::getStepTimerId,stepTimerId)
                        .eq(PlateGridTimer::getPlateId,plateId));
    }

    @Override
    public List<PlateGridTimer> selectListByStepTimerId(Long stepTimerId) {
        return plateGridTimerMapper.selectList(
                new LambdaQueryWrapper<PlateGridTimer>()
                        .eq(PlateGridTimer::getStepTimerId,stepTimerId));
    }

    @Override
    public List<Long> selectPlateGridIdList(Long plateId) {
        return plateGridTimerMapper.selectPlateGridIdList(plateId);
    }

    @Override
    public boolean deleteByExperimentId(Long experimentId) {
        List<Plate> plateList=plateMapper.selectList(
                new LambdaQueryWrapper<Plate>()
                        .eq(Plate::getExperimentId,experimentId)
                        .eq(Plate::getIsDelete,BoolEnum.FALSE.getValue()));
        List<Long> plateIds=plateList.stream().map(Plate::getId).distinct().collect(Collectors.toList());
        if(org.apache.commons.collections.CollectionUtils.isNotEmpty(plateIds)){
            return plateGridTimerMapper.delete(
                    new LambdaQueryWrapper<PlateGridTimer>()
                            .in(PlateGridTimer::getPlateId,plateIds))>0;
        }
        return true;
    }
}
